
import { _decorator, Component, AnimationClip, Node, Sprite, UITransform, Animation, animation, Vec3, SpriteFrame } from 'cc';
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';
import ResourceManager from '../../Runtime/ResourceManager';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, TILE_TYPE_ENUM } from '../../Enum';
import EventManager from '../../Runtime/EventManager';
import { PlayerStateMachine } from './PlayerStateMachine';
import { EnityManager } from '../../Base/EntityManger';
import { DataManager } from '../../Runtime/DataManager';

const { ccclass, property } = _decorator;

const ANIMATION_SPEED = 1 / 8;

@ccclass('PlayerManager')
export class PlayerManager extends EnityManager {
   targetX: number = 0;
   targetY: number = 0;
   private readonly speed = 1 / 10;
   isMoving = false

   async init() {
      this.fsm = this.addComponent(PlayerStateMachine);
      await this.fsm.init();

      super.init({
         x: 0,
         y: 0,
         direction: DIRECTION_ENUM.TOP,
         state: ENTITY_STATE_ENUM.IDLE,
         type: ENTITY_TYPE_ENUM.PLAYER
      });

      this.targetX = this.x;
      this.targetY = this.y;
      EventManager.Instance.on(EVENT_ENUM.PLAYER_CTRL, this.move, this);
   }

   update() {
      this.updateXY();
      super.update();
   }

   updateXY() {
      if (this.targetX < this.x) {
         this.x -= this.speed;
      } else if (this.targetX > this.x) {
         this.x += this.speed;
      }

      if (this.targetY < this.y) {
         this.y -= this.speed;
      } else if (this.targetY > this.y) {
         this.y += this.speed
      }

      if (Math.abs(this.targetX - this.x) < 0.1 && Math.abs(this.targetY - this.y) < 0.1) {
         this.x = this.targetX;
         this.y = this.targetY;
      }
   }

   inputHandle(inputDirectioin: CONTROLLER_ENUM) {
      if (this.willBlock(inputDirectioin)) {
         console.log("block")
         return;
      }

      this.move(inputDirectioin)
   }

   willBlock(inputDirectioin: CONTROLLER_ENUM): boolean {
      const { targetX: x, targetY: y, direction } = this
      const { tileInfo } = DataManager.Instance;

      if (inputDirectioin === CONTROLLER_ENUM.TOP) {
         if (direction === DIRECTION_ENUM.TOP) {
            const playerNextY = y - 1;
            const weaponNextY = y - 2;
            //这个表明在坐标区域以外
            if (playerNextY < 0) {
               return true;
            }

            //人物坐标  枪的坐标
            const playerTile = tileInfo[x][playerNextY];
            const weaponTile = tileInfo[x][weaponNextY];

            if (playerTile && playerTile.moveable && (!weaponTile || weaponTile.turnable)) {
               //empty
            } else {
               return true;
            }
         }
      }

      return false;
   }


   move(inputDirectioin: CONTROLLER_ENUM) {
      //switch case 语句必须使用严格的 ==
      switch (inputDirectioin) {
         case CONTROLLER_ENUM.TOP:
            this.targetY -= 1;
            break;
         case CONTROLLER_ENUM.BOTTOM:
            this.targetY += 1;
            break;
         case CONTROLLER_ENUM.LEFT:
            this.targetX -= 1;
            break;
         case CONTROLLER_ENUM.RIGHT:
            this.targetX += 1;
            break;
         case CONTROLLER_ENUM.TURNLEFT:
            this.exchangedirectionLEFT()
      }
   }
   exchangedirectionLEFT() {
      this.fsm.setParams(PARAMS_NAME_ENUM.TURNLEFT, true);
      switch (this.direction) {
         case DIRECTION_ENUM.TOP:
            this.direction = DIRECTION_ENUM.LEFT
            break;
         case DIRECTION_ENUM.LEFT:
            this.direction = DIRECTION_ENUM.BOTTOM
            break;
         case DIRECTION_ENUM.BOTTOM:
            this.direction = DIRECTION_ENUM.RIGHT
            break;
         case DIRECTION_ENUM.RIGHT:
            this.direction = DIRECTION_ENUM.TOP
            break;
      }
   }

}


